﻿using System;
using SapaFinance.Domain.Extensions;
using SapaFinance.Domain.Model;
using Xunit;

namespace SapaFinance.Tests
{
    public class PairTest
    {
        public static readonly decimal DValue = new decimal(28.123456789);

        public Pair CreatePair()
        {
            var result = new Pair(new Usd(), new Rub());

            result.SetValue(DValue);
            return result;
        }
        [Fact]
        public void TestCreate()
        {
            var pair = CreatePair();
            Assert.Equal(new Usd(), pair.From);
            Assert.Equal(new Rub(), pair.To);
            Assert.Equal(DValue.RoundTo(DecimalSize.Four), pair.Value);
            pair.SetRoundSize(DecimalSize.Zero);
            Assert.Equal(new decimal(28).RoundTo(DecimalSize.Zero),pair.Value );
        }
        [Fact]
        public void TestRoundZero()
        {
            var result = CreatePair();
            result.SetRoundSize(DecimalSize.Zero);
            var inverse = result.Inverse();
            Assert.Equal(new Rub(), inverse.From);
            Assert.Equal(new Usd(), inverse.To);
            Assert.Equal((1 / DValue).RoundTo(DecimalSize.Zero), inverse.Value);
            var dirty = inverse.Inverse();
            Assert.Equal(result, dirty);
            Assert.Equal(dirty, dirty);
        }
        [Fact]
        public void TestRoundOne()
        {
            var result = CreatePair();
            result.SetRoundSize(DecimalSize.One);
            var inverse = result.Inverse();
            Assert.Equal(new Rub(), inverse.From);
            Assert.Equal(new Usd(), inverse.To);
            Assert.Equal((1 / DValue).RoundTo(DecimalSize.One), inverse.Value);
            var dirty = inverse.Inverse();
            Assert.Equal(result, dirty);
            Assert.Equal(dirty, dirty);
        }

        [Fact]
        public void TestRoundTwo()
        {
            var result = CreatePair();
            result.SetRoundSize(DecimalSize.Two);
            var inverse = result.Inverse();
            Assert.Equal(new Rub(), inverse.From);
            Assert.Equal(new Usd(), inverse.To);
            Assert.Equal((1 / DValue).RoundTo(DecimalSize.Two), inverse.Value);
            var dirty = inverse.Inverse();
            Assert.Equal(result, dirty);
            Assert.Equal(dirty, dirty);
        }
        [Fact]
        public void TestRoundThree()
        {
            var result = CreatePair();
            result.SetRoundSize(DecimalSize.Three);
            var inverse = result.Inverse();
            Assert.Equal(new Rub(), inverse.From);
            Assert.Equal(new Usd(), inverse.To);
            Assert.Equal((1 / DValue).RoundTo(DecimalSize.Three), inverse.Value);
            var dirty = inverse.Inverse();
            Assert.Equal(result, dirty);
            Assert.Equal(dirty, dirty);
        }
        [Fact]
        public void TestRoundFour()
        {
            var result = CreatePair();
            result.SetRoundSize(DecimalSize.Four);
            var inverse = result.Inverse();
            Assert.Equal(new Rub(), inverse.From);
            Assert.Equal(new Usd(), inverse.To);
            Assert.Equal((1 / DValue).RoundTo(DecimalSize.Four), inverse.Value);
            var dirty = inverse.Inverse();
            Assert.Equal(result, dirty);
            Assert.Equal(dirty, dirty);
        }
    }
}